﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using EngineOfEvermore.Common;
using EngineOfEvermore.Scene.Physics;

namespace EngineOfEvermore.Scene
{
    public sealed class SceneObject
    {
        #region Variables
        List<SceneObjectComponent> _components = new List<SceneObjectComponent>();
        SceneGraph _sceneGraph;
        Vector2d _position;
        float _rotation;
        Vector2d _linearVelocity;
        int _space;
        List<IPhysicsModifier> _physicsModifier = new List<IPhysicsModifier>();
        #endregion

        #region Events
        public event Action<SceneObject, Vector2d> PositionChanged;
        public event Action<SceneObject, float> RotationChanged;
        public event Action<SceneObject, Vector2d> LinearVelocityChanged;
        public event Action<SceneObject, int> SpaceChanged;
        public event Action<SceneObject, IPhysicsModifier> PhysicsModifierAdded;
        public event Action<SceneObject, IPhysicsModifier> PhysicsModifierRemoved;
        #endregion

        #region Properties
        public SceneGraph SceneGraph
        {
            get { return _sceneGraph; }
        }

        public Vector2d Position
        {
            get { return _position; }
            set
            {
                if ( _position != value )
                {
                    _position = value;

                    if ( PositionChanged != null )
                    {
                        PositionChanged( this, value );
                    }
                }
            }
        }

        public float Rotation
        {
            get { return _rotation; }
            set
            {
                if ( _rotation != value )
                {
                    _rotation = value;

                    if ( RotationChanged != null )
                    {
                        RotationChanged( this, value );
                    }
                }
            }
        }

        public Vector2d LinearVelocity
        {
            get { return _linearVelocity; }
            set
            {
                if ( _linearVelocity != value )
                {
                    _linearVelocity = value;

                    if ( LinearVelocityChanged != null )
                    {
                        LinearVelocityChanged( this, value );
                    }
                }
            }
        }

        public int Space
        {
            get { return _space; }
            set
            {
                Debug.Assert( value >= 0 );

                if ( _space != value )
                {
                    _space = value;

                    if ( SpaceChanged != null )
                    {
                        SpaceChanged( this, value );
                    }
                }
            }
        }
        #endregion

        #region Component Stuff
        public int ComponentCount
        {
            get { return _components.Count; }
        }

        public SceneObjectComponent GetComponent( int index )
        {
            Debug.Assert( index < _components.Count );
            return _components[index];
        }

        public T GetComponent<T> () where T : SceneObjectComponent {

            foreach ( SceneObjectComponent c in _components )
            {
                if ( c is T )
                {
                    return (T) c;
                }
            }

            return null;
        }

        public void AddComponent( SceneObjectComponent c )
        {
            if ( _sceneGraph == null || !_sceneGraph.TimeContext.IsUpdating )
            {
                _addComponent( c );
            }
            else
            {
                _sceneGraph.ScheduleComponentAddition( this, c );
            }
        }

        public void RemoveComponent( SceneObjectComponent c )
        {
            if ( _sceneGraph == null || !_sceneGraph.TimeContext.IsUpdating )
            {
                _removeComponent( c );
            }
            else
            {
                _sceneGraph.ScheduleComponentRemoval( this, c );
            }
        }

        public bool ContainsComponent( SceneObjectComponent c )
        {
            return _components.Contains( c );
        }
        #endregion

        #region PhysicsModifiers
        public IEnumerable<IPhysicsModifier> PhysicsModifiers
        {
            get { return _physicsModifier; }
        }

        public void AddPhysicsModifier( IPhysicsModifier mod )
        {
            _physicsModifier.Add( mod );

            if ( PhysicsModifierAdded != null )
            {
                PhysicsModifierAdded( this, mod );
            }
        }

        public void RemovePhysicsModifier( IPhysicsModifier mod )
        {
            if ( PhysicsModifierRemoved != null )
            {
                PhysicsModifierRemoved( this, mod );
            }

            _physicsModifier.Remove( mod );
        }
        #endregion

        #region Internal SceneGraph House Keeping 
        internal void OnAddToScene( SceneGraph scene )
        {
            Debug.Assert( _sceneGraph == null );

            _sceneGraph = scene;

            foreach ( SceneObjectComponent ec in _components )
            {
                ec.OnAddToScene( scene );
            }
        }

        internal void OnRemoveFromScene( SceneGraph scene )
        {
            Debug.Assert( _sceneGraph == scene );

            foreach ( SceneObjectComponent ec in _components )
            {
                ec.OnRemoveFromScene( scene );
            }

            _sceneGraph = null;
        }

        internal void UpdateXForm( float x, float y, float rot )
        {
            _position.X = x;
            _position.Y = y;
            _rotation = rot;
        }
        #endregion

        #region Private Methods
        void _addComponent( SceneObjectComponent c )
        {
            Debug.Assert( c.Owner == null );

            _components.Add( c );

            c.OnAddToSceneObject( this );

            foreach ( SceneObjectComponent sibling in _components )
            {
                if ( c != sibling )
                {
                    sibling.OnSiblingAdded( c );
                    c.OnSiblingAdded( sibling );
                }
            }

            if ( _sceneGraph != null )
            {
                c.OnAddToScene( _sceneGraph );
            }
        }

        void _removeComponent( SceneObjectComponent c )
        {
            Debug.Assert( c.Owner == this );
            Debug.Assert( _components.Contains( c ) );

            if ( _sceneGraph != null )
            {
                c.OnRemoveFromScene( _sceneGraph );
            }

            foreach ( SceneObjectComponent sibling in _components )
            {
                if ( c != sibling )
                {
                    c.OnSiblingRemoved( sibling );
                    sibling.OnSiblingRemoved( c );
                }
            }

            c.OnRemoveFromSceneObject( this );
            _components.Remove( c );
        }
        #endregion
    }
}
